package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.21
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/admin")
@Slf4j
public class DtsAdminAdminController {

    @Autowired
    DtsAdminService adminService;

    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin){
        log.debug("admin--delete:{"+ admin +"}");

        adminService.deleteById(admin.getId());

        log.info("删除管理员成功");

        return ResponseUtil.ok();

    }

    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin){
        log.debug("admin --update:{"+ admin +"}");

        DtsAdmin dd = adminService.update(admin);

        log.info("修改管理员成功");
        return ResponseUtil.ok(dd);
    }

    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin){
        log.debug("admin--add : {"+admin +"}");

        DtsAdmin admin1 = adminService.insert(admin);
        log.info("添加管理员成功");
        return ResponseUtil.ok(admin1);
    }

    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
            @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit , String username , String sort , String order){

        //进行分页查询
       IPage<DtsAdmin> iPage =  adminService.queryPage(page,limit,username,sort,order);


        Map<String,Object> data = new HashMap<>();

        //封装
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return ResponseUtil.ok(data);
    }
}
